{% extends 'base.html' %}
{% load static %}
{% load user_filter %}
{% block title %}用户管理{% endblock %}
{% block custome_css %}{% endblock %}
{% block content %}
    <div class="card card-mb0">

        <div class="card-toolbar clearfix">

            <!-- 搜索表单 -->
            {% if request|has_permission:'search' %}
                <form class="pull-right search-bar" method="get" action="" role="form">
                    <div class="input-group input-group-sm">
                        <input type="text" class="form-control" placeholder="请输入关键词..."
                               name="search" value="{% if h_search %}{{ h_search }}{% endif %}">
                        <span class="input-group-btn">
                          <button type="submit" class="btn btn-success" title="点击开始搜索">搜索</button>
                        </span>
                    </div>
                </form>
            {% endif %}

            <!-- 按钮 -->
            <div class="toolbar-btn-action">
                {% if request|has_permission:'create' %}
                    <a class="btn btn-primary m-r-5 btn-sm" onclick="add_obj()"><i class="mdi mdi-plus"></i> 新增</a>
                {% endif %}
            </div>
        </div>

        <div class="card-body card-body-p0">

            <!-- 表格 -->
            <table class="table table-striped table-bordered table-hover table-condensed table-one">
                <thead>
                    <tr>
                        <th style="text-align: center;">序号</th>
                        <th>用户名</th>
                        <th>姓名</th>
                        <th>部门</th>
                        <th>手机号码</th>
                        <th>邮箱</th>
                        <th style="text-align: center;">性别</th>
                        <th style="text-align: center;">状态</th>
                        <th>角色</th>
                        <th>更新时间</th>
                        <th style="text-align: center;width: 160px;min-width: 160px;">操作</th>
                    </tr>
                </thead>
                <tbody>
                {% for obj in p_objs.object_list %}
                    <tr>
                        <td style="text-align: center;">{{ forloop.counter|add:p_start }}</td>
                        <td>{{ obj.username }}</td>
                        <td>{{ obj.name }}</td>
                        <td>{{ obj.dept.name }}</td>
                        <td>{{ obj.mobile|default_if_none:'' }}</td>
                        <td>{{ obj.email }}</td>
                        <td style="text-align: center;">{{ obj.get_gender_display }}</td>
                        <td style="text-align: center;">
                            {% ifequal obj.is_active True %}
                                <span class="text-success">启用</span>
                            {% else %}
                                <span class="text-danger">禁用</span>
                            {% endifequal %}
                        </td>
                        <td>
                            {% for role in obj.role.all %}
                                {% ifequal role.key 'admin' %}
                                    <span class="label label-danger">{{ role.name }}</span>
                                {% else %}
                                    {% ifequal role.key 'admin-dept' %}
                                        <span class="label label-danger">{{ role.name }}</span>
                                    {% else %}
                                        {% ifequal role.key 'publish' %}
                                            <span class="label label-warning">{{ role.name }}</span>
                                        {% else %}
                                            <span class="label label-success">{{ role.name }}</span>
                                        {% endifequal %}
                                    {% endifequal %}
                                {% endifequal %}
                            {% endfor %}
                        </td>
                        <td>{{ obj.modify_datetime|date:"Y-m-d H:i" }}</td>
                        <td style="text-align: center;width: 160px;min-width: 160px;">
                            <div class="btn-group">
                                {% if request|has_permission:'update' %}
                                    <a class="btn btn-xs btn-warning m-r-5" href="#!" onclick="modify_obj({{ obj.id }});" title="修改" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
                                {% endif %}
                                {% if request|has_permission:'export' %}
                                    <a class="btn btn-xs btn-info m-r-5" href="#!" onclick="export_obj({{ obj.id }});" title="导出" data-toggle="tooltip"><i class="mdi mdi-export"></i></a>
                                {% endif %}
                                {% if request|has_permission:'reset' %}
                                    <a class="btn btn-xs btn-danger m-r-5" href="#!" onclick="reset_password({{ obj.id }});" title="重置密码" data-toggle="tooltip"><i class="mdi mdi-lock-reset"></i></a>
                                {% endif %}
                                {% if request|has_permission:'delete' %}
                                    <a class="btn btn-xs btn-danger" href="#!" onclick="delete_obj({{ obj.id }});" title="删除" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            <!-- 表格分页 -->
            <div class="table-pagination">
                <span>显示 {{ p_objs.start_index }} - {{ p_objs.end_index }} ，&nbsp;共 {{ p_sum }} 项</span>
                <ul class="pagination">
                    {% if p_objs.has_previous %}
                        <li><a href="?{{ p_objs.previous_page_number.querystring }}">&laquo;</a></li>
                    {% else %}
                        <li><a href="javascript:return false;" style="cursor: not-allowed;"><i style="opacity: 0.2">&laquo;</i></a>
                        </li>
                    {% endif %}
                    {% for page in p_objs.pages %}
                        {% if page %}
                            {% ifequal page p_objs.number %}
                                <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li>
                            {% else %}
                                <li><a href="?{{ page.querystring }}">{{ page }}</a></li>
                            {% endifequal %}
                        {% else %}
                            <li><a>...</a></li>
                        {% endif %}
                    {% endfor %}
                    {% if p_objs.has_next %}
                        <li><a href="?{{ p_objs.next_page_number.querystring }}">&raquo;</a></li>
                    {% else %}
                        <li><a href="javascript:return false;" style="cursor: not-allowed;"><i style="opacity: 0.2">&raquo;</i></a>
                        </li>
                    {% endif %}
                </ul>
            </div>

        </div>

    </div>
{% endblock %}

{% block custome_js %}
    <script>
        function add_obj() {
            $.ajax({
                type: "GET",
                url: '{% url 'systems:user_add' %}',
                success: function(callback){
                    $.confirm({
                        title: '新增',
                        content: callback,
                        columnClass: 'col-md-6 col-md-offset-3',
                        buttons: {
                            formSubmit: {
                                text: '保存',
                                btnClass: 'btn-success btn-w-sm',
                                action: function () {
                                    var form = this.$content.find('#form-add');
                                    if(form.length > 0){
                                        $.ajax({
                                            type: form.attr('method'),
                                            url: form.attr('action'),
                                            data: new FormData(form[0]),  // 为了兼容数据和文件均能正常提交，将data从form.serialize()更改为form_data
                                            processData: false,           // 告诉jQuery不要去处理发送的数据
                                            contentType: false,           // 告诉jQuery不要去设置Content-Type请求头
                                            success: function (resp) {
                                                if (resp.code === "200") {
                                                    location.href = "{% url 'systems:user_list' %}";
                                                } else {
                                                    $.alert(resp.msg);
                                                }
                                            },
                                            error: function () {
                                                lightyear.notify('系统异常', 'warning', 100);
                                            }
                                        });
                                    }
                                    return false;
                                }
                            },
                            cancel: {
                                text: '取消'
                            },
                        }
                    });
                },
                error: function(){
                    lightyear.notify('系统异常', 'warning', 100);
                }
            });
        }

        function modify_obj(i_id) {
            $.ajax({
                type: "GET",
                url: '{% url 'systems:user_detail' %}',
                data: {'id': i_id},
                success: function(callback){
                    $.confirm({
                        title: '修改',
                        content: callback,
                        columnClass: 'col-md-6 col-md-offset-3',
                        buttons: {
                            formSubmit: {
                                text: '保存',
                                btnClass: 'btn-success btn-w-sm',
                                action: function () {
                                    var form = this.$content.find('#form-add');
                                    if(form.length > 0){
                                        $.ajax({
                                            type: form.attr('method'),
                                            url: "{% url 'systems:user_modify' %}",
                                            data: new FormData(form[0]),  // 为了兼容数据和文件均能正常提交，将data从form.serialize()更改为form_data
                                            processData: false,                     // 告诉jQuery不要去处理发送的数据
                                            contentType: false,                     // 告诉jQuery不要去设置Content-Type请求头
                                            success: function (resp) {
                                                if (resp.code === "200") {
                                                    location.href = "{% url 'systems:user_list' %}";
                                                } else {
                                                    $.alert(resp.msg);
                                                }
                                            },
                                            error: function () {
                                                lightyear.notify('系统异常', 'warning', 100);
                                            }
                                        });
                                    }
                                    return false;
                                }
                            },
                            cancel: {
                                text: '取消'
                            },
                        }
                    });
                },
                error: function(){
                    lightyear.notify('系统异常', 'warning', 100);
                }
            })
        }

        function delete_obj(i_id) {
            var url = "{% url 'systems:user_delete' 0 %}";
            $.confirm({
                title: '提示',
                content: '确认要删除该用户吗？',
                type: 'red',
                typeAnimated: true,
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'btn-danger btn-w-sm',
                        action: function(){
                            $.ajax({
                                type: "GET",
                                url: url.replace('0', i_id),
                                success: function (callback) {
                                    if (callback.code === "200") {
                                        location.href = "{% url 'systems:user_list' %}";
                                    } else {
                                        $.alert(callback.msg);
                                    }
                                },
                                error: function () {
                                    lightyear.notify('系统异常', 'warning', 100);
                                }
                            })
                        }
                    },
                    cancel: {
                        text: '取消'
                    }
                }
            });
        }

        function reset_password(i_id) {
            $.ajax({
                type: "GET",
                url: '{% url 'systems:user_pwd_modify' %}',
                data: {'id': i_id},
                success: function (callback) {
                    $.confirm({
                        title: '重置密码',
                        content: callback,
                        columnClass: 'col-md-6 col-md-offset-3',
                        buttons: {
                            somethingElse: {
                                text: '重置为默认密码',
                                btnClass: 'btn-blue',
                                action: function(){
                                    var url = "{% url 'systems:user_pwd_reset' 0 %}";
                                    $.ajax({
                                        type: "GET",
                                        url: url.replace('0', i_id),
                                        success: function (callback) {
                                            if (callback.code === "200") {
                                                lightyear.notify(callback.msg, 'success', 100);
                                            } else {
                                                $.alert(callback.msg);
                                            }
                                        },
                                        error: function () {
                                            lightyear.notify('系统异常', 'warning', 100);
                                        }
                                    })
                                }
                            },
                            formSubmit: {
                                text: '保存',
                                btnClass: 'btn-success btn-w-sm',
                                action: function () {
                                    var form = this.$content.find('#form-add');
                                    if(form.length > 0){
                                        $.ajax({
                                            type: form.attr('method'),
                                            url: form.attr('action'),
                                            data: new FormData(form[0]),  // 为了兼容数据和文件均能正常提交，将data从form.serialize()更改为form_data
                                            processData: false,           // 告诉jQuery不要去处理发送的数据
                                            contentType: false,           // 告诉jQuery不要去设置Content-Type请求头
                                            //cache: false,               // 上传文件不需要缓存
                                            success: function (resp) {
                                                if (resp.code === "200") {
                                                    lightyear.notify(resp.msg, 'success', 100);
                                                } else {
                                                    $.alert(resp.msg);
                                                }
                                            },
                                            error: function () {
                                                lightyear.notify('系统异常', 'warning', 100);
                                            }
                                        });
                                        this.close();
                                    }
                                    return false;
                                }
                            },
                            cancel: {
                                text: '取消'
                            },
                        }
                    });
                },
                error: function () {
                    lightyear.notify('系统异常', 'warning', 100);
                }
            })
        }

        function export_obj(i_id) {
            $.ajax({
                type: "GET",
                url: '{% url 'systems:user_export' %}',
                data: {'user_id': i_id},
                success: function(callback){
                    $.confirm({
                        title: '导出任务',
                        content: callback,
                        columnClass: 'col-md-6 col-md-offset-3',
                        buttons: {
                            formSubmit: {
                                text: '确定',
                                btnClass: 'btn-info btn-w-sm',
                                action: function () {
                                    var form = this.$content.find('#form-export');
                                    if(form.length > 0){
                                            var v_stime = this.$content.find('#start_datetime').val();
                                            var v_etime = this.$content.find('#end_datetime').val();
                                            if(v_stime==null || v_stime===""){
                                                $.alert('请选择开始时间');
                                                return false;
                                            }
                                            if(v_etime==null || v_etime===""){
                                                $.alert('请选择结束时间');
                                                return false;
                                            }
                                            var v_sdate = new Date(Date.parse(v_stime));
                                            var v_edate = new Date(Date.parse(v_etime));
                                            if (v_sdate.getTime() > v_edate.getTime()){
                                                $.alert("结束时间不得早于开始时间");
                                                return false;
                                            }
                                            this.$content.find('#form-export').submit();

                                            {#$.alert("正在导出中，完成后系统会自动下载.");#}

                                            lightyear.notify('正在导出中，完成后系统会自动下载.', 'success', 1000);
                                            {#return true;#}
                                    }
                                    return false;
                                }
                            },
                            cancel: {
                                text: '取消'
                            },
                        }
                    });
                },
                error: function(){
                    lightyear.notify('系统异常', 'warning', 100);
                }
            })
        }

    </script>
{% endblock %}
